/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "image_widget.h"

#include <QMouseEvent>
#include <QAction>
#include <QMenu>
#include <QClipboard>
#include <QApplication>
#include <QFileDialog>

namespace Ui {

Image_Widget::Image_Widget(QWidget* _parent) : QLabel(_parent) {
	this->grabMouse();
	
	menu = new QMenu(this);
	action_clipboard = new QAction(tr("Copy to Clipboard"),menu);
	action_save_image = new QAction(tr("Save Image as"),menu);
	
	menu->addAction(action_clipboard);
	menu->addAction(action_save_image);
	
	connect(action_clipboard, SIGNAL(triggered()), this, SLOT(copy_to_clipboard()));
	connect(action_save_image, SIGNAL(triggered()), this, SLOT(save_image()));
}

Image_Widget::~Image_Widget(void) {
	
}

void Image_Widget::copy_to_clipboard(void) {
	if(pixmap() != 0) {
		QClipboard* clipboard = QApplication::clipboard();
		clipboard->setPixmap(*pixmap(),QClipboard::Clipboard);
	}
}
void Image_Widget::save_image(void) {
	if(pixmap() != 0) {
		QString filename = QFileDialog::getSaveFileName(this,tr("Save Image as"), QDir::currentPath(),tr("Image Files (*.png, *.jpeg, *.jpg, *.bmp, *.ppm, *.xpm, *.xbm)"));
		if (!filename.isNull())
			pixmap()->save(filename);
	}
}

void Image_Widget::mousePressEvent(QMouseEvent* _event) {
	if (_event->button() == Qt::LeftButton) {
		emit clicked(_event->x(),_event->y());
	}	
	if (_event->button() == Qt::RightButton) {
		menu->popup(QPoint(_event->globalX() ,_event->globalY()));
	}
}


}
